package music;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import util.MovingFile;
import util.UnZip;
import util.Zip;

import mp3.ModifyFolder;

public class ManageMusic {
    
//    private static final String inputFolder = "/Volumes/ENT/$Music/Originals";
//    private static final String tempFolder = "/Volumes/ENT/$Music/Extracted";
//    private static final String outputFolder = "/Volumes/ENT/$Music/Music";
//    private static final String outputFolder_ZIP = "/Volumes/ENT/$Music/MusicZipped";
	
	private String inputFolder;// = "/Volumes/ENT/$Music Test/Originals 1";
    private String outputFolder;// = "/Volumes/ENT/$Music Test/Music 1";
    private String tempFolder;
    private String outputFolder_ZIP;
    
    private final String MACOSX = "__MACOSX";
    
    public ManageMusic(String input, String output, String zipped) {
    	this.inputFolder = input;
    	this.outputFolder = output;
    	this.tempFolder = output+".temp";
    	this.outputFolder_ZIP = zipped;
    }
 
    /**
     * Extracts all zip files into folders
     * Checks subfolders for zipfiles as well
     * Currently only exctracts zip files to temp folder
     * ENHANCE: copy non zip conent to temp to modify as well.
     * 
     * @throws IOException
     */
	public void extractMusic(boolean deleteOriginals) throws IOException {
		UnZip unZip = new UnZip();
		File folder = new File(inputFolder);
		List<String> allZipFiles = new ArrayList<String>();
		unZip.listZipFilesForFolder(folder, allZipFiles);
		for(String zipFile : allZipFiles) {
			unZip.extractByLoopAllFiles(zipFile, tempFolder, deleteOriginals);
		}
	}
	
	/**
	 * Edits all MP3 tags and copies them to output folder
	 * Edits only if it's not already clean 
	 * ENHANCE 
	 * 1) change the folders based on Album and year
	 * 2) Edit based on Database
	 * @throws Exception
	 */
	public List<String> editMusic() throws Exception {
		List<String> duplicateFolders = new ArrayList<String>();
		
		File folder = new File(tempFolder);
		ModifyFolder mf = new ModifyFolder();
		List<String> allMp3 = new ArrayList<String>();
		mf.listFilesForFolder(folder, allMp3);
		for(String mp3Path : allMp3) {
			if(mp3Path.contains(MACOSX)) {
				//TODO remove the folder
				String mp3Folder = mp3Path.substring(0, mp3Path.indexOf(MACOSX)) + MACOSX;
				MovingFile.delete(new File(mp3Folder));
			} else {
				mf.cleanMP3File(mp3Path);
			}
		}
		
		//move clean files to music folder, so you don't clean the same files multiple times
		File output = new File(tempFolder);
		duplicateFolders = MovingFile.copyDirectoryDonotOverride(output, outputFolder, new File(outputFolder), duplicateFolders);
		MovingFile.delete(output);
		
//		File folder = new File(outputFolder);
//		ModifyFolder mf = new ModifyFolder();
//		List<String> allMp3 = new ArrayList<String>();
//		mf.listFilesForFolder(folder, allMp3);
//		for(String mp3Path : allMp3) {
//			mf.cleanMP3File(mp3Path);
//		}
		
		return duplicateFolders;
		
	}
	
	/**
	 * Zips folders
	 */
	public void zipEdited() {
		if(outputFolder_ZIP!=null && !outputFolder_ZIP.isEmpty()) {
			Zip appZip = new Zip();
			List<String> listForFirstLevelFolders = appZip.listForFirstLevelFolders(new File(outputFolder));
			for(String folder : listForFirstLevelFolders) {
				appZip.setSourceFolder(folder);
				String[] splitter = folder.split("\\/");
		    	appZip.zipIt(outputFolder_ZIP + "/" + splitter[splitter.length-1] + ".zip"); //WITH LOCATION AND NAME
			}
		}
	}
	

}
